"""
量子近似优化算法实现 - 优化版
用于解决组合优化问题
"""

# 使用绝对导入
from core.hybrid_engine import QuantumHybridEngine
from qiskit import QuantumCircuit
from qiskit.quantum_info import SparsePauliOp
from typing import Union, Dict, List
import numpy as np
import logging

class QAOASolver:
    """量子近似优化算法求解器"""
    
    def __init__(self,
                 cost_operator: Union[SparsePauliOp, List],
                 mixer_operator: Union[SparsePauliOp, List] = None,
                 p: int = 1):
        """
        初始化QAOA求解器
        
        :param cost_operator: 代价哈密顿量 (SparsePauliOp或Pauli字符串列表)
        :param mixer_operator: 混合哈密顿量 (默认为所有X算子的和)
        :param p: 算法层数
        """
        # 处理cost operator输入
        if isinstance(cost_operator, list):
            self.cost_operator = SparsePauliOp.from_list(cost_operator)
        else:
            self.cost_operator = cost_operator
            
        # 处理mixer operator输入
        if mixer_operator is None:
            # 默认混合哈密顿量: 所有X算子的和
            pauli_str = "X" * self.cost_operator.num_qubits
            self.mixer_operator = SparsePauliOp.from_list([(pauli_str, 1.0)])
        elif isinstance(mixer_operator, list):
            self.mixer_operator = SparsePauliOp.from_list(mixer_operator)
        else:
            self.mixer_operator = mixer_operator
            
        self.p = p
        self.num_qubits = self.cost_operator.num_qubits
        self.engine = None
        self.logger = logging.getLogger(__name__)
        
        # 验证哈密顿量量子比特数匹配
        if self.mixer_operator.num_qubits != self.num_qubits:
            raise ValueError(f"混合哈密顿量量子比特数({self.mixer_operator.num_qubits})与代价哈密顿量量子比特数({self.num_qubits})不匹配")

    # 其余代码保持不变...
